require('./index.css');
require('page/common/top/index.js');
var _util = require('util/util.js');
var _account_service = require('service/account-service.js');

var userLogin = {
    init : function(){
        this.loadAccountInfo();
        this.bindEvents();
        return this;
    },
    bindEvents : function(){
        $('#isVaild').hide();
        $('#repeatedPassword').on('blur',function(){
            var password1 = $('#password').val();
            var password2 = $('#repeatedPassword').val();
            if(password1 != password2){
                $('#isVaild').show();
            }
            else{
                $('#isVaild').hide();
            }
        })
    },
    loadAccountInfo : function(){
        $('#editAccount').on('click',function(){
            var password1 = $('#password').val();
            var password2 = $('#repeatedPassword').val();
            if(password1 != password2){
                $('#isVaild').show();
            }
            else{
                var account = {
                    username :  $('#username').val(),
                    password :  password1
                } 
                
                
                _account_service.editAccount(account,
                    function(res){
                        window.location.href = './catalog-main.html';
                    },
                    function(errorMsg){
                        if(errorMsg === "用户名不存在"){
                            $('#isVaild').show().text("用户名不存在");
                        }
                    })
            }
        })

    }
};

module.exports = userLogin.init();